package com.liuyongyi.livepush.camerapush;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.view.SurfaceHolder;

import com.liuyongyi.livepush.camerapush.natives.PusherNative;

/**
 * Created by liuyongyi on 2017/12/31.
 */

public class VideoPusher extends IPusher implements SurfaceHolder.Callback, Camera.PreviewCallback {
    private SurfaceHolder surface;
    private Camera camera;
    private VideoParms videoParms;
    private byte[] buffer;
    private boolean isPusher = false;
    private PusherNative pusherNative;

    public VideoPusher(SurfaceHolder surface,VideoParms videoParms,PusherNative pusherNative){
        this.surface = surface;
        this.videoParms = videoParms;
        this.pusherNative= pusherNative;
        surface.addCallback(this);
    }

    @ Override
    public void startPusher() {
        pusherNative.setVideoOptions(videoParms.getWidth(),videoParms.getHeight(),videoParms.getBitrate(),videoParms.getFps());
        isPusher = true;
    }

    @Override
    public void stopPusher() {
        isPusher = false;
    }

    @Override
    public void release() {
        stopCamera();
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        startCamera();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//        stopCamera();
    }
    //切换摄像头
    public void switChangeCamera(){
        if(videoParms.getCameraId() == Camera.CameraInfo.CAMERA_FACING_BACK){
            videoParms.setCameraId(Camera.CameraInfo.CAMERA_FACING_FRONT);
        }else {
            videoParms.setCameraId(Camera.CameraInfo.CAMERA_FACING_BACK);
        }
        stopCamera();
        startCamera();
    }

    private void startCamera(){
        try {
            camera = Camera.open(videoParms.getCameraId());

            Camera.Parameters parameters = camera.getParameters();
            //设置相机参数
            parameters.setPictureFormat(ImageFormat.NV21);//预览图像的像素格式
            parameters.setPreviewSize(videoParms.getWidth(),videoParms.getHeight());//预览画面的宽高

            camera.setParameters(parameters);

            parameters.setPreviewFpsRange(videoParms.getFps()-1,videoParms.getFps());

            camera.setPreviewDisplay(surface);
            //获取视频预览的视频数据
            buffer = new byte[videoParms.getWidth() * videoParms.getHeight() * 4];
            camera.addCallbackBuffer(buffer);
            camera.setPreviewCallback(this);
            camera.startPreview();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopCamera(){
        if(camera != null){
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    }


    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        if(this.camera != null){
            this.camera.addCallbackBuffer(buffer);
        }
        //方法能不断的呗调用
        if(isPusher){
            //回调函数中获取图像数据，然后给native代码编码
            pusherNative.sendVideo(bytes);
        }
    }
}
